09. Exercise: Event Logging Practice

Exercise: Event Logging Practice

Exercise Code

Starting Code for this Exercise

Task Description:

Here's some additional logging practice. Using what you've learned, add code to Green Thumb to log the events below with Firebase Analytics. Check that the events are recorded in logcat to verify that you are logging at the correct time with the correct data.

Task List:

Task Feedback:

Great job!

Solution: [L03.03-Solution-EventLoggingPractice][Diff]

Here is the code that implements logging the three specified events:

Log when a plant's detail is viewed:

public static void logEventViewItem(Context context, Plant plant) {
   Bundle params = new Bundle();
   params.putInt(FirebaseAnalytics.Param.ITEM_ID, plant.id);
   params.putString(FirebaseAnalytics.Param.ITEM_NAME, plant.name);
   FirebaseAnalytics.getInstance(context)
           .logEvent(FirebaseAnalytics.Event.VIEW_ITEM, params);
}
// Called in onLoadFinished in PlantDetailActivity, once the Plant object is queried and initialized

Log when the shopping cart is viewed (i.e., when the user begins the checkout process).

public static void logEventBeginCheckout(Context context) {
   FirebaseAnalytics.getInstance(context).logEvent(
           FirebaseAnalytics.Event.BEGIN_CHECKOUT, null);
}
// Called in onCreate in ShoppingCartActivity

Log when the shopping cart is checked out.

public static void logEventEcommercePurchase(Context context) {
   FirebaseAnalytics.getInstance(context).logEvent(
           FirebaseAnalytics.Event.ECOMMERCE_PURCHASE, null);
}
// Called in the checkout button click listener in ShoppingCartActivity